

#include "ly/pcell_header.hpp"
#include "ly/pcell_variant.hpp"

namespace ly {

// ----------------------------------------------------------------------------------------
//  PCellParametersCompareFunc implementation

// bool PCellParametersCompareFunc::operator()(const pcell_parameters_type* a, const pcell_parameters_type* b) const {
//   if (a->size() != b->size()) {
//     return a->size() < b->size();
//   }

//   for (size_t i = 0; i < a->size(); ++i) {
//     if ((*a)[i] < (*b)[i]) {
//       return true;
//     } else if ((*b)[i] < (*a)[i]) {
//       return false;
//     }
//   }

//   return false;
// }

// ----------------------------------------------------------------------------------------
//  PCellHeader implementation

PCellHeader::PCellHeader(size_t pcell_id, const std::string& name, PCellDeclaration* declaration)
    : mp_declaration(declaration), m_pcell_id(pcell_id), m_name(name) {
  if (mp_declaration) {
    mp_declaration->add_ref();
  }
}

PCellHeader::PCellHeader(const PCellHeader& d) : mp_declaration(d.mp_declaration), m_pcell_id(d.m_pcell_id), m_name(d.m_name) {
  if (mp_declaration) {
    mp_declaration->add_ref();
  }
}

PCellHeader::~PCellHeader() {
  if (mp_declaration) {
    mp_declaration->release_ref();
  }
  mp_declaration = 0;
}

void PCellHeader::declaration(PCellDeclaration* declaration) {
  if (mp_declaration) {
    mp_declaration->release_ref();
  }
  mp_declaration = declaration;
  if (declaration) {
    mp_declaration->add_ref();
  }
}

// std::vector<unsigned int> PCellHeader::get_layer_indices(ly::Layout& layout, const ly::pcell_parameters_type& parameters,
//                                                          ly::ImportLayerMapping* layer_mapping) {
//   if (!declaration()) {
//     return std::vector<unsigned int>();
//   }

//   ly::DirectLayerMapping direct_layer_mapping(&layout);
//   if (!layer_mapping) {
//     layer_mapping = &direct_layer_mapping;
//   }

//   std::vector<ly::PCellLayerDeclaration> layer_declarations = mp_declaration->get_layer_declarations(parameters);

//   std::vector<unsigned int> layer_indices;
//   layer_indices.reserve(layer_declarations.size());
//   for (size_t i = 0; i < layer_declarations.size(); ++i) {
//     std::pair<bool, unsigned int> lm(false, 0);
//     if (layer_declarations[i] != ly::LayerProperties()) {
//       lm = layer_mapping->map_layer(layer_declarations[i]);
//     }
//     if (lm.first) {
//       layer_indices.push_back(lm.second);
//     } else {
//       layer_indices.push_back(layout.waste_layer());
//     }
//   }

//   return layer_indices;
// }

PCellVariant* PCellHeader::get_variant(ly::Layout& /*layout*/, const pcell_parameters_type& parameters) {
  // variant_map_t::iterator v = m_variant_map.find(&parameters);
  // if (v != m_variant_map.end()) {
  //   return v->second;
  // } else {
  //   return 0;
  // }

  return 0;
}

void PCellHeader::unregister_variant(PCellVariant* variant) {
  // variant_map_t::iterator v = m_variant_map.find(&variant->parameters());
  // assert(v != m_variant_map.end());
  // m_variant_map.erase(v);
}

void PCellHeader::register_variant(PCellVariant* variant) {
  // variant_map_t::iterator v = m_variant_map.find(&variant->parameters());
  // assert(v == m_variant_map.end());
  // m_variant_map.insert(std::make_pair(&variant->parameters(), variant));
}

}  // namespace ly
